package br.com.lazyprogrammer.app.ffo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import br.com.lazyprogrammer.app.ffo.R;
import br.com.lazyprogrammer.app.ffo.model.MenuItem;

/**
 * Created by LimaoGames on 12/10/2014.
 */
public class MenuAdapter extends BaseAdapter {

    private Context context;

    private List<MenuItem> items = new ArrayList<MenuItem>();

    public MenuAdapter(Context context) {
        this.context = context;

        String[] labels = context.getResources().getStringArray(R.array.menu_item);
        for (int i = 0; i < labels.length; i++)
            items.add(new MenuItem(R.drawable.ic_launcher, labels[i]));
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public MenuItem getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_menu, container, false);

            holder.icon = (ImageView) view.findViewById(R.id.icon);
            holder.label = (TextView) view.findViewById(R.id.label);

            view.setTag(holder);
        }
        else
            holder = (ViewHolder) view.getTag();

        holder.label.setText(getItem(position).getLabel());
        holder.icon.setImageResource(getItem(position).getResId());

        return view;
    }

    class ViewHolder {
        ImageView icon;
        TextView label;
    }
}
